#include<memory>
#include"Listener.hpp"
#include"HandlerConnection.hpp"
#include"Reactor.hpp"
#include "PackageParse.hpp"


int main(int argc,char*argv[])
{
    if(argc!=2)
    {
        std::cerr<<"usage"<<argv[0]<<" local port "<<std::endl;
        exit(0);
    }
    uint16_t port=std::stoi(argv[1]);
    EnableScreen();
    InetAddr addr("127.0.0.1",port);
    
    Listener listener(port);
    PackageParse packageparse;
    
    HandlerConnection handler(std::bind(&PackageParse::Excute,&packageparse,std::placeholders::_1));

    std::unique_ptr<Reactor> R=std::make_unique<Reactor>();
    
    R->SetOnConnect(std::bind(&Listener::Accepter,&listener,std::placeholders::_1));
    R->SetOnNormalHandler(
        std::bind(&HandlerConnection::HandlerRecver,&handler,std::placeholders::_1),
        std::bind(&HandlerConnection::HandlerSender,&handler,std::placeholders::_1),
        std::bind(&HandlerConnection::HandlerExcepter,&handler,std::placeholders::_1)        
    );

    R->AddConnection(listener.ListenSockfd(),EPOLLIN|EPOLLET,addr,ListenConnection);
    
    R->Dispatchar();
    
    return 0;
}